package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WordListFragment extends ListFragment {
	OnWordChangeListener mListener;
	
	public interface OnWordChangeListener{
		public void onWordChanged(int index);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try{
			mListener = (OnWordChangeListener) activity;
		}catch(ClassCastException e){
			throw new ClassCastException("해당 액티비티는 OnWordChangeListener를 구현해야 함");
		}
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		String[] WORD = getResources().getStringArray(R.array.word);
		setListAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, WORD));
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		mListener.onWordChanged(position);
	}
	
}
